/*
 *Copyright (c) 2011 EdRP, Dayalbagh Educational Institute.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL ETRG OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors: Members of EdRP, Dayalbagh Educational Institute
 */

package in.ac.dei.edrp.cms.daoimpl.employee;

import java.util.Date;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;


public class sendmail {
	/**
     * @param args
     */
    public static void main(String text, String to, String from, String subject)
        throws Exception {
        // Get system properties
        Properties properties = System.getProperties();
        
        Logger loggerObject = Logger.getLogger(sendmail.class);

        // Setup mail server
        properties.setProperty("mail.smtp.port", "25");
        properties.setProperty("mail.smtp.host", "smtp.gmail.com");
        properties.setProperty("mail.smtp.starttls.enable", "true");
        properties.setProperty("mail.smtp.startssl.enable", "true");
        properties.setProperty("mail.smtp.auth", "true");

        //	        properties.put("smtp.mail.yahoo.com ","465");
        //	        properties.put("smtp.mail.yahoo.com.startssl.enable", "true");
        //	        properties.put("smtp.mail.yahoo.com.auth", "true");

        // Get the default Session object.
        Authenticator auth = new MyAuthenticator();
        Session session = Session.getDefaultInstance(properties, auth);

        
            // Create a default MimeMessage object.
            MimeMessage message = new MimeMessage(session);

            // Set the RFC 822 "From" header field using the 
            // value of the InternetAddress.getLocalAddress method.
            message.setFrom(new InternetAddress(from));

            // Add the given addresses to the specified recipient type.
            message.addRecipient(Message.RecipientType.TO,
                new InternetAddress(to));

            message.setSubject(subject);

            message.setText(text);
            message.setSentDate(new Date());
            System.out.println(message.getSentDate());

            // Send message
            Transport.send(message);

        loggerObject.info("Message Send.....");
    }

    static class MyAuthenticator extends Authenticator {
    	String sep=System.getProperty("file.separator");
    	ResourceBundle resourceBundle = ResourceBundle.getBundle("in"+sep+"ac"+sep+"dei"+sep+"edrp"+sep+"cms"+sep+"databasesetting"+sep+"MessageProperties",
    			new Locale("en", "US"));
    	public PasswordAuthentication getPasswordAuthentication() {
            String emailId = resourceBundle.getString("emailId");
            String password = resourceBundle.getString("password");
            return new PasswordAuthentication(emailId, password);
        }
    }
}
